import { reqGetCode,reqUserRegister,reqUserLogin } from "@/api"
const state = {
    phone: '',
    code: '',
    token:''
}
const actions = {
    async getCode(context,phone) {
        state.phone=phone
        const result = await reqGetCode(phone)
        context.commit('GETCODE',result.data,phone)
    },
    async userRegister(context,data){
        const result = await reqUserRegister(data)
        if(result.code === 200){
            return 'ok'
        }else{
            return Promise.reject(new Error(result.message))
        }
    },
    async userLogin(context,data){
        const result = await reqUserLogin(data)
        if(result.code === 200){
            context.commit('USERLOGIN',result)
            return 'ok'
        }else{
            return Promise.reject(new Error(result.message))
        }
    }
}
const mutations = {
    GETCODE(state,data,phone){
        state.code=data
        state.phone=phone
    },
    USERLOGIN(state,data){
        state.token=data.data.token
        let userToken = localStorage.getItem('USER_TOKEN')
        if(!userToken || state.token!=userToken){
            localStorage.setItem('USER_TOKEN',state.token)
        }
    }
}
const getters = {

}
export default {
    state,
    actions,
    mutations,
    getters
}